#include <stdio.h>
#include "LPC23xx.H" 
#include "bas_fun.h" 
#include "uart_fun.h"
#include "aio_fun.h"
#include "rtu_file.h"

#ifdef AIH_NUM

void ad_int_timer0(void) 
{

}

void ad_pro(void)
{

}

#endif	  


///////////////////////////////////////////////////////
void AI_alm_check(void);
void DOS_input(void);
void AOH_pro(void);	
void DOH_pro(int);
void DIH_buf_pro(int);

#ifdef CDR_FUN
#include "cdr_fun.h"
#endif
void sys_alarm(void)
{
    int alm;
	
	alm=Tsync_alarm;
#ifdef DRS_FUN 	
	if (DRS[0].RsAlm==1) alm=1;
#endif	
	DI_set_bit(RSF_SYS_ALARM,alm,0,0);
}

void aio_sec_pro(void)
{	
  AI_alm_check();
	DOS_input();
  sys_alarm();
}	

void aio_pro(void)
{
  if (aio_dly) {
	  AOH_pro();	
		DOH_pro(aio_dly);			
	  DIH_buf_pro(aio_dly);		
		aio_dly=0;	
	}
#ifdef AIH_NUM	
	//ad_pro();	
#endif		
}	

unsigned short dihms=0;
static U8 dih_ostus[DIH_BNUM];
void timer0_aio_pro(void)
{
 
	
#ifdef DIH_NUM      
  dihms++;
	if (dihms>=DIH_DETECT_UNIT) {
	  dihms=0;
    if ((++DIH_buf_h)>=DIH_BUF_NUM) DIH_buf_h=0;			   
		if (!ioport_stop) {
		  DIH_BUF[DIH_buf_h][0]=dih_ostus[0]=~DIH1_IN;			
			DIH_BUF[DIH_buf_h][1]=dih_ostus[1]=~DIH2_IN; 				
			DIH_BUF[DIH_buf_h][2]=dih_ostus[2]=~DIH3_IN;
		  DIH_BUF[DIH_buf_h][3]=dih_ostus[3]=~DIH4_IN;
			DIH_BUF[DIH_buf_h][4]=dih_ostus[4]=~DIH5_IN;
			DIH_BUF[DIH_buf_h][5]=dih_ostus[5]=~DIH6_IN;				
		  }
	    else {
		    DIH_BUF[DIH_buf_h][0]=dih_ostus[0];				
				DIH_BUF[DIH_buf_h][1]=dih_ostus[1];					
				DIH_BUF[DIH_buf_h][2]=dih_ostus[2];	
				DIH_BUF[DIH_buf_h][3]=dih_ostus[3];	
				DIH_BUF[DIH_buf_h][4]=dih_ostus[4];	
				DIH_BUF[DIH_buf_h][5]=dih_ostus[5];					
		  }
	    DIH_buf_rdy++;

#ifdef EVNB_NUM	
    if (Tms>=1000) Tms=999;
	  (DIH_DT+DIH_buf_h)->secs=Tsecs;
	  (DIH_DT+DIH_buf_h)->ms=Tms;
#endif  
  }
#endif 

#ifdef AIH_NUM	
	//ad_int_timer0();
#endif	
}
